#include "RealGraph.h"

RealGraph::RealGraph(vector <NODE> &N, vector<EDGE>&E, map<string,int> &NI){
	Nodes=N;
	Edges=E;
	NodeIndex=NI;
};
/**     \brief    Given the type of a node, its name, and index, a node is added to Real Graph.
*/
void RealGraph::addNode(int TypeOfNode,string data,int map) {
	string type;      
	if(TypeOfNode==0) 
		type="Variable";
	else if(TypeOfNode==1) 
		type="Literal";
	else if(TypeOfNode==2) 
		type="Expression";
	else 
		type="Statement";
	int fakeindex=OccurrenceCounter[data];
	ostringstream tempindex;
	tempindex<<fakeindex;
	string fakename=data+tempindex.str();
	OccurrenceCounter[data]++;
	NodeIndex[fakename]=map;
	IndexToFake[map]=fakename;
	NODE *temp =new NODE(data,type,fakename);
	Nodes.push_back(*temp);
}
/**     \brief    Given both fake and real name of source and destination, index of source, and weight of edge , an edge is added to Real Graph.
*/
void RealGraph::addEdge(int mapFakeSource,int mapFakeDest,string realSource,string realDest,string EdgeWeight,int i) {

	string SourceFake=IndexToFake[mapFakeSource];
	string DestFake=IndexToFake[mapFakeDest];
	EDGE* edge= new EDGE(realSource,realDest,EdgeWeight,SourceFake,DestFake);
	Edges.push_back(*edge);
	Nodes[i].adjEdges.push_back(*edge); 

}
/**     \brief    Print Function that outputs the nodes stored in the Nodes Vector
*/
void RealGraph::print(){
	for (int i=0;i<Nodes.size();i++)
		Nodes[i].print();
}
void RealGraph::test(){
	for (int i=0;i<Nodes.size();i++)
		cout<<Nodes[i].Fake<<"  "<<NodeIndex[Nodes[i].Fake]<<" "<<i<<endl;
}
void RealGraph::VisitNode(const char * name, const char* type, const char * fake){
}
void RealGraph::VisitEdge(const char * source, const char * dest,const char* weight){
}
/**    \brief     Traversal Function that goes over a graph by visiting its nodes and all its adjacent ones. The adjacent nodes are found using the Edges vector: All nodes adjacent to a node n1 are in the vector appended to the entry of the adjacency list indexed by n1.
*/
void RealGraph::traverse(){
	bool *Visited=new bool[Nodes.size()];
	string program="Program";
	string st="Statement";
	VisitNode((const char*)program.c_str(),(const char*)st.c_str(),(const char *)program.c_str());
	for (int i=0;i<Nodes.size();i++){
		VisitNode((const char*)Nodes[i].data.c_str(),(const char*)Nodes[i].type.c_str(),(const char*)Nodes[i].Fake.c_str());
		Visited[i]=false;
	}
	for (int i=0;i<Edges.size();i++){
		VisitEdge((const char*)Edges[i].Fakesource.c_str(),(const char*)Edges[i].FakeDest.c_str(),(const char *)Edges[i].weight.c_str());
		if (!(Edges[i].weight=="right" && Edges[i].source==":="))
			Visited[NodeIndex[Edges[i].FakeDest]]=true;
	}
	string bl="blue";
	for (int i=0;i<Nodes.size();i++){
		if (!Visited[i]){
			VisitEdge((const char *)program.c_str(),(const char*)Nodes[i].Fake.c_str(),(const char*)bl.c_str());
		}
	}
}
